package com.kirk.crawler.utils;


import com.kirk.crawler.constant.ResourceConstants;
import com.kirk.crawler.core.loader.ResouceLoader;
import org.apache.log4j.Logger;

import javax.swing.*;
import java.awt.*;
import java.util.HashMap;

/**
 *  <p> ImageManager.java </p>
 *
 * @Author      kirkliu
 * @Date        2020-08-29 22:33
 * @version     1.0.0
 * @Description 图片资源管理工具类
 */
public class ImageManager {

	protected static Logger logger = Logger.getLogger(ImageManager.class);

	private static HashMap<String, ImageIcon> imageRegistry = new HashMap<String, ImageIcon>();

	public static HashMap<String, ImageIcon> getImageRegistry() {
		if (imageRegistry == null) {
			imageRegistry = new HashMap<String, ImageIcon>();
		}
		return imageRegistry;
	}

	/**
	 * 根据图片名获取图标资源对象
	 * @param imageName 图标名称
	 * @return 图标资源对象
	 */
	public static ImageIcon getImageIcon(String imageName) {
		return getImageIcon(imageName, null);
	}

	/**
	 * 根据图片名和描述获取图标资源对象
	 * @param imageName 名称
	 * @param description 描述
	 * @return 资源对象
	 */
	public static ImageIcon getImageIcon(String imageName, String description) {
		ImageIcon getImageIcon = imageRegistry.get(imageName);
		if (getImageIcon == null) {
			getImageIcon = description == null ? new ImageIcon(ResouceLoader.getResouce(imageName)) :
					new ImageIcon(ResouceLoader.getResouce(imageName), description);
			imageRegistry.put(imageName, getImageIcon);
		}
		return getImageIcon;
	}

	public static Image getImage(String imageName) {
		return getImageIcon(imageName).getImage();
	}
	
	public static ImageIcon getImageIconByShortName(String imageName) {
		return getImageIcon( ResourceConstants.PACKAGE_IMAGE_ICONS +
				ResourceConstants.SYSTEM_SEPARATOR + imageName);
	}
	
	public static Image getImageByShortName(String imageName) {
		return getImageIcon(ResourceConstants.PACKAGE_IMAGE_ICONS +
				ResourceConstants.SYSTEM_SEPARATOR + imageName).getImage();
	}
	
	public static String getImagePath(String imageName){
		return ResouceLoader.getResouce(ResourceConstants.PACKAGE_IMAGE_ICONS +
				ResourceConstants.SYSTEM_SEPARATOR + imageName).getPath();
	}

}
